<?php
class Data_Item 
{
    protected $_commTableName;
    protected $_commPrimaryKey;

    public function __construct () 
    {
        // 
        $this->_commPrimaryKey = 'id';
    }

    public function hasRecord ($wheres)
    {
        $query = DB::select()
            ->from($this->_commTableName)
            ->limit(1);

        foreach ($wheres as $w)
        {
            if (is_array($w))
            {
                $query->where($w[0], $w[1], $w[2]);
            }
            else
            {
                $query->where($wheres[0], $wheres[1], $wheres[2]);
                break;
            }
        }

        $query = $query->execute();
        return $query->count() > 0;
    }

    public function insert ($row)
    {
        list($insert_id, $rows) = DB::insert($this->_commTableName)
            ->columns (array_keys($row))
            ->values  (array_values($row))
            ->execute();

        return $insert_id;
    }

    public function update ($data, $wheres, $limit = 1)
    {
        $query = DB::update($this->_commTableName)
            ->set($data);

        if ($limit > 0)
            $query->limit($limit);

        foreach ($wheres as $w)
        {
            if (is_array($w))
            {
                $query->where($w[0], $w[1], $w[2]);
            }
            else
            {
                $query->where($wheres[0], $wheres[1], $wheres[2]);
                break;
            }
        }

        $rows = $query->execute();
        return $rows;
    }

    public function delete ($where, $limit = 1)
    {
        $query = DB::delete($this->_commTableName);

        if (! empty($where))
        {
            if (is_array($where) && is_array($where[0]))
            {
                foreach ($where as $rs)
                    $query->where($rs[0], $rs[1], $rs[2]);
            }
            else
            {
                $query->where($where[0], $where[1], $where[2]);
            }
        }

        if ($limit > 0)
            $query->limit($limit);
     
        return $query->execute();
    }

    public function record ($id)
    {
        $query = DB::select()
            ->from($this->_commTableName)
            ->where($this->_commPrimaryKey, '=', $id)
            ->limit(1)
            ->execute();

        return $query->current();
    }

    public function detail ($id = NULL)
    {
        $id = $id > 0 ? $id : $this->_nid;
        return $this->item(array($this->_commPrimaryKey, '=', $id));
    }

    //
    // 返回表格记录详情
    // @return array
    //
    public function item ($wheres, $order_by = NULL, $offset = 0, $limit = 10)
    {
        $query = DB::select()
            ->from($this->_commTableName)
            ->limit(1);

        foreach ($wheres as $w)
        {
            if (is_array($w))
            {
                $query->where($w[0], $w[1], $w[2]);
            }
            else
            {
                $query->where($wheres[0], $wheres[1], $wheres[2]);
                break;
            }
        }

        if (is_array($order_by) && ! empty($order_by))
        {
            $query->order_by($order_by[0], $order_by[1]);
        }

        if ($offset > 0)
            $query->offset($offset);

        if ($limit > 0)
            $query->limit($limit);

        $query = $query->execute();
        return $query->current();
    }

    public function items ($wheres, $order_by = NULL, $offset = 0, $limit = 10)
    {
        $query = DB::select()
            ->from($this->_commTableName);

        if (! empty($wheres))
        {
	        foreach ($wheres as $w)
	        {
	            if (is_array($w))
	            {
	                $query->where($w[0], $w[1], $w[2]);
	            }
	            else
	            {
	                $query->where($wheres[0], $wheres[1], $wheres[2]);
	                break;
	            }
	        }
        }

        if (is_array($order_by) && ! empty($order_by))
        {
            $query->order_by($order_by[0], $order_by[1]);
        }

        if ($offset > 0)
            $query->offset($offset);

        if ($limit > 0)
            $query->limit($limit);

        $query = $query->execute();
        return $query->as_array();
    }


    public function incColumnValue ($nid, $type, $n)
    {
        if ($n > 0)
        {
            DB::update($this->_commTableName)
            ->set(array($type => DB::expr("`$type` + $n")))
            ->where($this->_commPrimaryKey, '=', $nid)
            ->execute();
        }
        else
        {
            $n = abs($n);
            DB::update($this->_commTableName)
            ->set(array($type => DB::expr("`$type` - $n")))
            ->where($this->_commPrimaryKey, '=', $nid)
            ->execute();
        }
    }

    public function getColumnValue ($nid, $columnKey)
    {
        $v = DB::select($columnKey)
            ->from($this->_commTableName)
            ->where($this->_commPrimaryKey, '=', $nid)
            ->limit(1)
            ->execute()
            ->get($columnKey);

        return $v;
    }

    public function counts($where)
    {
        $pkey  = $this->_commPrimaryKey;
        $query = DB::select(DB::expr("count(`$pkey`) as counts"))
            ->from($this->_commTableName);

        if (! empty($where))
        {
            if (is_array($where) && is_array($where[0]))
            {
                foreach ($where as $rs)
                    $query->where($rs[0], $rs[1], $rs[2]);
            }
            else
            {
                $query->where($where[0], $where[1], $where[2]);
            }
        }

        return (int) $query->execute()->get('counts');
    }
}
